/***********************************************************

 The information in this document is proprietary
 to VeriSign and the VeriSign Product Development.
 It may not be used, reproduced or disclosed without
 the written approval of the General Manager of
 VeriSign Product Development.

 PRIVILEGED AND CONFIDENTIAL
 VERISIGN PROPRIETARY INFORMATION
 REGISTRY SENSITIVE INFORMATION

 Copyright (c) 2010 VeriSign, Inc.  All rights reserved.

 ***********************************************************/
package com.ordern.classification;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeIterator;
import org.htmlparser.util.NodeList;

/**
 * @author pdash
 * 
 */
public class InputParser {

	private static final String DOMAIN_PREFIX = "domain:";
	private static final String PAGE_PREFIX = "page:";

	private static final NodeFilter META_TAG_FILTER = new TagNameFilter("meta");
	private static final NodeFilter TITLE_TAG_FILTER=new TagNameFilter("title");
	private static final NodeFilter BODY_TAG_FILTER = new TagNameFilter("body");

	public List<Domain> parseInput(Reader testFileReader, String baseDir)
			throws Exception {
		BufferedReader reader = new BufferedReader(testFileReader);
		String line = null;
		List<Domain> domains = new ArrayList<Domain>();
		Domain currentDomain = null;
		boolean homePage = false;
		while ((line = reader.readLine()) != null) {
			if (line.startsWith(DOMAIN_PREFIX)) {
				if (currentDomain != null) {
					domains.add(currentDomain);
				}
				currentDomain = new Domain();
				currentDomain.setName(line.substring(DOMAIN_PREFIX.length())
						.trim());
				homePage = true;
			} else if (line.startsWith(PAGE_PREFIX)) {
				String pageFilePath = line.substring(PAGE_PREFIX.length());
				if (!pageFilePath.startsWith("/")) {
					pageFilePath = baseDir + "/" + pageFilePath;
				}
				Page p = getHTMLPageInfo(pageFilePath);
				if (homePage) {
					currentDomain.setTitle(p.getTitle());
					currentDomain.setBody(p.getBody());
					currentDomain.setMetaContent(p.getMetaContent());
					currentDomain.setLocation( pageFilePath );
					homePage = false;
				}
				currentDomain.addChildPage(p);

			}
		}

		return domains;

	}

	public Page getHTMLPageInfo(String filePath) throws Exception {
		StringBuffer stb = new StringBuffer();
		File testDataFile = new File(filePath);
		BufferedReader reader = new BufferedReader(new FileReader(testDataFile));
		String line = null;
		Domain currentDomain = null;
		while ((line = reader.readLine()) != null) {
			stb.append(line);
		}
		Parser p = Parser.createParser(stb.toString(), null);
		NodeList titleList =new NodeList();
		NodeList bodyList =new NodeList();
		NodeList metaList =new NodeList();
		for (NodeIterator e = p.elements (); e.hasMoreNodes (); )
		{
			Node n = e.nextNode ();
			n.collectInto (titleList, TITLE_TAG_FILTER);
			n.collectInto (bodyList, BODY_TAG_FILTER);
			// n.collectInto (metaList, META_TAG_FILTER);
			n.collectInto( metaList, HtmlHelper.correctMetaFilter() );

		}
		
		Page page = new Page();
		page.setTitle(getContent(titleList));
		page.setBody(getContent(bodyList));
		page.setMetaContent(getMetaContent(metaList));
		return page;
	}

	private String getMetaContent(NodeList n) throws Exception {
		StringBuffer stb = new StringBuffer();
		for (int i = 0; i < n.size(); i++) {
			if(n.elementAt(i) instanceof Tag)
			{
				Tag tag = (Tag)n.elementAt(i);
				stb.append(tag.getAttribute("content"));
			}
		}
		
		return stb.toString();
	}
	
	private String getContent(NodeList n) throws Exception {
		StringBuffer stb = new StringBuffer();
		for (int i = 0; i < n.size(); i++) {
			stb.append(HtmlHelper.cleanseJerico(n.elementAt(i).toHtml()));
		}
		return stb.toString().replaceAll("<http.+?>", "");
	}


	

}
